package org.darcy.controller.admin.article;

import java.util.Map;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.darcy.entity.BizArticle;
import org.darcy.framework.annotation.BussinessLog;
import org.darcy.framework.blog.business.enums.BaiduPushTypeEnum;
import org.darcy.framework.blog.business.enums.ConfigKeyEnum;
import org.darcy.framework.blog.business.enums.ResponseStatus;
import org.darcy.framework.blog.business.util.BaiduPushUtil;
import org.darcy.framework.model.PageResult;
import org.darcy.framework.model.ResponseVO;
import org.darcy.framework.util.ResultUtil;
import org.darcy.framework.util.SessionUtil;
import org.darcy.framework.util.UrlBuildUtil;
import org.darcy.model.BizArticleModel;
import org.darcy.service.BizArticleService;
import org.darcy.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;

import lombok.extern.slf4j.Slf4j;

/**
 * 文章管理
 */
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

	@Autowired
	private BizArticleService articleService;

	@Autowired
	private SysConfigService configService;

	@RequiresPermissions("articles")
	@PostMapping("/list")
	public PageResult list(BizArticleModel vo) {
		Long userId = SessionUtil.getCommonUserId();
		vo.setUserId(userId);
		IPage<BizArticleModel> pageInfo = articleService.query(vo);
		return ResultUtil.tablePage(pageInfo);
	}

	@RequiresPermissions(value = { "article:batchDelete", "article:delete" }, logical = Logical.OR)
	@PostMapping(value = "/remove")
	@BussinessLog("删除文章[{1}]")
	public ResponseVO remove(Long[] ids) {
		if (null == ids) {
			return ResultUtil.error(500, "请至少选择一条记录");
		}
		for (Long id : ids) {
			articleService.delete(id);
		}
		return ResultUtil.success("成功删除 [" + ids.length + "] 篇文章");
	}

	@RequiresPermissions("article:get")
	@PostMapping("/get/{id}")
	@BussinessLog("获取文章[{1}]详情")
	public ResponseVO get(@PathVariable Long id) {
		BizArticleModel data = this.articleService.getDetail(id);
		return ResultUtil.success(null, data);
	}

	@RequiresPermissions(value = { "article:edit", "article:publish" }, logical = Logical.OR)
	@PostMapping("/save")
	@BussinessLog("发布文章")
	public ResponseVO edit(BizArticle article, Long[] tags, MultipartFile file) {
		articleService.publish(article, tags, file);
		return ResultUtil.success(ResponseStatus.SUCCESS);
	}

	@RequiresPermissions(value = { "article:top", "article:recommend" }, logical = Logical.OR)
	@PostMapping("/update/{type}")
	@BussinessLog("修改文章[{2}]的状态[{1}]")
	public ResponseVO update(@PathVariable("type") String type, Long id) {
		articleService.updateTopOrRecommendedById(type, id);
		return ResultUtil.success(ResponseStatus.SUCCESS);
	}

	@RequiresPermissions(value = { "article:batchPush", "article:push" }, logical = Logical.OR)
	@PostMapping(value = "/pushToBaidu/{type}")
	@BussinessLog("推送文章[{2}]到百度站长平台")
	public ResponseVO pushToBaidu(@PathVariable("type") BaiduPushTypeEnum type, Long[] ids) {
		if (null == ids) {
			return ResultUtil.error(500, "请至少选择一条记录");
		}
		Map config = configService.getConfigs();
		String siteUrl = "www.xxx.com";
		StringBuilder params = new StringBuilder();
		for (Long id : ids) {
			params.append(siteUrl).append("/article/").append(id).append("\n");
		}
		// urls: 推送, update: 更新, del: 删除
		String url = UrlBuildUtil.getBaiduPushUrl(type.toString(), siteUrl,
				(String) config.get(ConfigKeyEnum.BAIDU_PUSH_TOKEN.getKey()));
		String result = BaiduPushUtil.doPush(url, params.toString(),
				(String) config.get(ConfigKeyEnum.BAIDU_PUSH_COOKIE.getKey()));
		log.info(result);
		JSONObject resultJson = JSONObject.parseObject(result);

		if (resultJson.containsKey("error")) {
			return ResultUtil.error(resultJson.getString("message"));
		}
		return ResultUtil.success(null, result);
	}

	@RequiresPermissions(value = { "article:publish" }, logical = Logical.OR)
	@PostMapping(value = "/batchPublish")
	@BussinessLog("批量发布文章[{1}]")
	public ResponseVO batchPublish(Long[] ids) {
		if (null == ids) {
			return ResultUtil.error(500, "请至少选择一条记录");
		}
		articleService.batchUpdateStatus(ids, true);
		return ResultUtil.success("批量发布完成");
	}
}
